package liuch.JMS;

import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.Destination;

public class JmsTemplateUtil {

    /**
     * spring container
     */
    private static ApplicationContext applicationContext;

    /**
     * jmsTemplate bean name
     */
    public static final String JMS_TEMPLATE_NAME = "jmsTemplate";

    private static JmsTemplate jmsTemplate;

    public static void setApplicationContext(ApplicationContext applicationContext) {
        JmsTemplateUtil.applicationContext = applicationContext;
        JmsTemplateUtil.jmsTemplate = (JmsTemplate) applicationContext.getBean(JMS_TEMPLATE_NAME);
    }

    /**
     * send message to activeMQ
     */
    public static void sendMsg(Destination destination, final Object object) {
        if (object == null || jmsTemplate == null || destination == null) {
            return;
        }
        if (object instanceof String) {
            jmsTemplate.convertAndSend(destination, object.toString());
        } else {
            jmsTemplate.convertAndSend(destination, object);
        }
    }


}
